// pages/class/details/details.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scan: false
  },
  showaudio() {
    this.setData({
      bannerimg: false
    }, () => {
      const videoContext = wx.createVideoContext("video", this);
      videoContext.requestFullScreen();
    })
  },
  gowebview() {
    wx.setClipboardData({
      data: this.data.c_data.down_url,
      success: (res) => {
        this.Global.showMsg("复制成功")
      },
      fail: (res) => {
        this.Global.showMsg("复制失败")
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.Api = app.Global.Api;
    this.Global = app.Global;
    if (options.scan !== undefined) {
      var scan = decodeURIComponent(options.scan);
      var scanid = decodeURIComponent(options.id);
      app.globalData.scan = scan;
      app.globalData.scanid = scanid;
    }  else {
      app.globalData.scan = -1;
      app.globalData.scanid = -1;
    }
    this.setData({
      ...options
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    //判断是否登入
    var scan = app.globalData.scan;
    this.Global.getWxUser().then((obj) => {
      console.log(obj)
      if (obj.id) {
        this.setData({
          is_load: true,
          uid: obj.id
        }, () => {
          this.init(this.data.id);
        })
        this.Global.hideLoginDialog();
      } else {
        console.log("未登入")
        this.Global.showLoginDialog();
        return;
      }
    })
  },
  //课程详情接口
  init(id) {
    var scan = app.globalData.scan;
    var scanid = app.globalData.scanid;
    console.log(scan, scanid)
    var rep = {
      uid: this.data.uid,
      id,
      scan
    }
    console.log(rep)
    this.Api.getCoursedetails(rep).then(res => {
      if (res.status == "success") {
        this.setData({
          c_data: res.data
        }, (() => {
          this.Global.wxParse.wxParse('content', 'html', this.data.c_data.content, this, 15);
        }))
        if (res.data.is_new == 1) {
            wx.showModal({
              title: '提示',
              content: `观看此课程需要${res.data.coin}个金币。`,
              showCancel: true,
              cancelText: '返回',
              cancelColor: '#9097A3',
              confirmText: '支付',
              confirmColor: '#3778FF',
              success: (o) => {
                if (o.confirm) {
                  //点击支付
                  var rep = {
                    uid: this.data.uid,
                    pid: this.data.id
                  }
                  this.Api.payCourse(rep).then(obj => {
                    if (obj.status == "success") {
                      this.Global.showMsg(obj.msg);
                    } else {
                      this.Global.showMsg(obj.msg).then(() => {
                        wx.switchTab({
                          url: '/pages/class/class',
                        })
                      })
                    }
                  })
                } else {
                  wx.switchTab({
                    url: '/pages/class/class',
                  })
                }
              },
            })
        } else {
          //无须支付金币
        }
      } else {
        //获取数据失败
        this.Global.showMsg(res.msg).then(() => {
          wx.switchTab({
            url: '/pages/class/class',
          })
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }

})